
# 序列上索引值迭代，想在迭代一个序列的同时跟踪正在被处理的元素索引
def demo_1():
    my_list = ['a', 'b', 'c']
    for idx, val in enumerate(my_list):
        print(idx, val)
    # 设定开始序号为1
    for idx, val in enumerate(my_list, 1):
        print(idx, val)

# 在遍历文件时想在错误消息中使用行号定位
def parse_data(filename):
    with open(filename, 'rt') as f:
        for lineno, line in enumerate(f, 1):
            fields = line.split()
            try:
                count = int(fields[1])
            except ValueError as e:
                print('Line {}: Parse error: {}'.format(lineno, e))

def demo_2():
    data = [(1, 2), (3, 4), (5, 6), (7, 8)]
    for n, (x, y) in enumerate(data, 1):
        print(n, x, y)

if __name__ == '__main__':
    # demo_1()
    demo_2()